const ip = getApp().globalData.ip
Page({
	data: {
		swiperCurrent: 0,
		ip: getApp().globalData.ip
	},
	onLoad() {
		// 调试头部样式
		this.getStyle()
		// 获取图片
		this.getImg()
		// 获取商品
		this.getGoods()
	},
	// 调试头部样式
	getStyle() {
		let menuButtonObject = wx.getMenuButtonBoundingClientRect();
		wx.getSystemInfo({
			success: res => {
				// console.log(res)
				let statusBarHeight = res.statusBarHeight,
					navTop = menuButtonObject.top, //胶囊按钮与顶部的距离
					navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2; //导航高度
				// console.log(menuButtonObject)
				this.setData({
					navHeight: navHeight + 'px',
					statusBarHeight: statusBarHeight + 'px',
					logoWidth: menuButtonObject.height + 'px',
					logoLeft: res.windowWidth - menuButtonObject.right + 'px',
					logoTop: menuButtonObject.top + 'px'
				})
			},
			fail(err) {
				console.log(err);
			}
		})
	},
	// 获取图片
	getImg() {
		this.setData({
			swiperInfo: [
				ip + 'swiper/1.jpg',
				ip + 'swiper/2.jpg',
				ip + 'swiper/3.jpg',
			],
			btn: [{
					text: "小米上新",
					url: ip + 'btn/a1.png'
				},
				{
					text: "手机",
					url: ip + 'btn/a2.png'
				},
				{
					text: "手机新品",
					url: ip + 'btn/a3.png'
				},
				{
					text: "Note 9",
					url: ip + 'btn/a4.png'
				},
				{
					text: "家电",
					url: ip + 'btn/a5.png'
				},
				{
					text: "小米秒杀",
					url: ip + 'btn/a6.png'
				},
				{
					text: "小米众筹",
					url: ip + 'btn/a7.png'
				},
				{
					text: "电视",
					url: ip + 'btn/a8.png'
				},
				{
					text: "笔记本",
					url: ip + 'btn/a9.png'
				},
				{
					text: "智能",
					url: ip + 'btn/a10.png'
				}
			]
		})
	},
	// 获取商品
	getGoods() {
		wx.request({
			url: ip + 'goodsGet',
			method: "POST",
			success: res => {
				console.log(res.data.info)
				let rank = []
				let phone = []
				let home = []
				let ai = []
				for (let i = 0; i < 3; i++) {
					rank.push(res.data.info[i])
				}
				for (let i = 0; i < res.data.info.length; i++) {
					if (res.data.info[i].type == "xiaomi" || res.data.info[i].type == "redmi") {
						phone.push(res.data.info[i])
					}
					if (res.data.info[i].type == "home") {
						home.push(res.data.info[i])
					}
					if (res.data.info[i].type == "car" || res.data.info[i].type == "book") {
						ai.push(res.data.info[i])
					}
				}
				this.setData({
					goods: res.data.info,
					swiperHeight: Math.ceil(res.data.info.length / 2),
					rank,
					phone,
					home,
					ai
				})
			}
		})
	},
	changeSwiper(e) {
		this.setData({
			swiperCurrent: parseInt(e.currentTarget.dataset.info)
		})
		this.setSwiperHeight()
	},
	changeCurrent(e) {
		this.setData({
			swiperCurrent: e.detail.current
		})
		this.setSwiperHeight()
	},
	setSwiperHeight() {
		switch (this.data.swiperCurrent) {
			case 0:
				this.setData({
					swiperHeight: Math.ceil(this.data.goods.length / 2)
				})
				break;
			case 1:
				this.setData({
					swiperHeight: Math.ceil(this.data.phone.length / 2)
				})
				break;
			case 2:
				this.setData({
					swiperHeight: Math.ceil(this.data.home.length / 2)
				})
				break;
			case 3:
				this.setData({
					swiperHeight: Math.ceil(this.data.ai.length / 2)
				})
				break;
			case 4:
				this.setData({
					swiperHeight: Math.ceil(this.data.ai.length / 2)
				})
				break;
			case 5:
				this.setData({
					swiperHeight: Math.ceil(this.data.home.length / 2)
				})
				break;
		}
	},
	toDetail(e) {
		wx.navigateTo({
			url: '../detail/detail?id='+e.currentTarget.dataset.id,
		})
	}
})